/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package handlers;

import java.util.HashMap;
import java.util.Iterator;
import java.io.Serializable;

import jktest.JKMainGui;

/**
 * 
 * Class that provides a means to see which tests have been completed
 * 
 * @author Royce Feng
 */
public class JKTestHandler implements Serializable {

    private HashMap<String,Boolean> resultMap = new HashMap<String, Boolean>();
    private JKMainGui parent;
    
    /**
     * 
     * Main Constructor with parent and test parameters
     * 
     * @param _parent The parent class
     * @param panels The test keys/parameters
     */
    public JKTestHandler ( JKMainGui _parent, String[] panels ) {
        parent = _parent;
        for ( int i = 0; i < panels.length; i++ ) {
            resultMap.put(panels[i], false );
        }
    }

    /**
     *
     * Allows the tests to be restarted
     *
     * @param panels The test keys/parameters
     */
    public void restartHash( String[] panels ) {
        for ( int i = 0; i < panels.length; i++ ) {
            resultMap.put(panels[i], false );
            parent.resetValue(panels[i]);
        }
    }
    
    /**
     * 
     * A method to save the test result
     * 
     * @param source The hash key
     * @param value The hash value
     */
    public void Hash ( String source, Boolean value ) {
        resultMap.put(source, value);
    }
    
    /**
     * 
     * Sees if all tests have been completed
     * 
     * @return True if all tests have been completed. False if they have not.
     */
    public boolean CanClose() {
        if ( resultMap.isEmpty() ) {
            return false;
        } else {
            boolean returnval = true;
            Iterator<String> iter = resultMap.keySet().iterator();
            while ( iter.hasNext() ) {
                String key = iter.next();
                returnval = returnval && resultMap.get(key);
            }
            return returnval;
        }
    }
    
}
